package com.qf.joint.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.joint.pojo.Address;
import com.qf.joint.pojo.Company;
import com.qf.joint.service.CompanyService;
import com.qf.joint.utils.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Zhy
 * Date on 2020/9/17  13:45
 * @description
 */
@Controller
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    //前台 根据id查询公司信息
    @RequestMapping(value = "/findById/{cid}", method = RequestMethod.GET)
    @ResponseBody
    public List<Company> findById(@PathVariable("cid") Integer cid){
        List<Company> list = new ArrayList<>();
        list.add(companyService.findById(cid));
        return list;
    }

    //后台查询所有
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public Info<Company> selectAll2(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                    @RequestParam(name = "limit", required = true, defaultValue = "4") int limit){
        int count = companyService.selectAll().size();
        PageHelper.startPage(page,limit);
        List<Company> list = companyService.selectAll();
        PageInfo<Company> pageInfo = new PageInfo<>(list);
        Info<Company> info = new Info<>();
        if (null!=list && list.size()>0){
            info.setCode(0);
            info.setData(list);
            info.setCount(count);//数据库数据总数
            System.out.println("controller1-info="+info);
            return info;
        }
        info.setCode(1);
        info.setMsg("没有查询到数据");
        System.out.println("controller2-info="+info);
        return info;//-/WEB-INF/view/test.jsp
    }

    //后台新增
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView insertOne(ModelAndView mv,Company company) throws UnsupportedEncodingException {
        System.out.println("controller-company="+company);
        String name = new String(company.getName().getBytes("iso-8859-1"),"utf-8");
        company.setName(name);
        String scale = new String(company.getScale().getBytes("iso-8859-1"),"utf-8");
        company.setScale(scale);
        String classify = new String(company.getClassify().getBytes("iso-8859-1"),"utf-8");
        company.setClassify(classify);
        String address = new String(company.getAddress().getBytes("iso-8859-1"),"utf-8");
        company.setAddress(address);
        String addpic = new String(company.getAddpic().getBytes("iso-8859-1"),"utf-8");
        company.setAddpic(addpic);
        String introduce = new String(company.getIntroduce().getBytes("iso-8859-1"),"utf-8");
        company.setIntroduce(introduce);
        String iacinfo = new String(company.getIacinfo().getBytes("iso-8859-1"),"utf-8");
        company.setIacinfo(iacinfo);
        int res = companyService.insertOne(company);
        System.out.println("controller2-res="+res);
        //mv.addObject("objLi", list);
        //mv.setViewName("redirect:/address/findAll");
        //mv.setViewName("redirect:/admin/address");
        mv.setViewName("redirect:/admin/admin");//暂时就让去主界面
        return mv;
//        return "redirect:/findAll";//-/WEB-INF/view/test.jsp
    }

    //后台修改
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    @ResponseBody
    public Info<Company> updateOne(Company company) throws UnsupportedEncodingException {
        //id,logo,公司名称,上市状态,规模,分类（写死）,地址,地址图片,介绍,工商信息
        //cid,logo,name,listedstate,scale,classify,address,addpic,introduce,iacinfo
        String name = new String(company.getName().getBytes("iso-8859-1"),"utf-8");
        company.setName(name);
        String scale = new String(company.getScale().getBytes("iso-8859-1"),"utf-8");
        company.setScale(scale);
        String classify = new String(company.getClassify().getBytes("iso-8859-1"),"utf-8");
        company.setClassify(classify);
        String address = new String(company.getAddress().getBytes("iso-8859-1"),"utf-8");
        company.setAddress(address);
        String addpic = new String(company.getAddpic().getBytes("iso-8859-1"),"utf-8");
        company.setAddpic(addpic);
        String introduce = new String(company.getIntroduce().getBytes("iso-8859-1"),"utf-8");
        company.setIntroduce(introduce);
        String iacinfo = new String(company.getIacinfo().getBytes("iso-8859-1"),"utf-8");
        company.setIacinfo(iacinfo);
        int res = companyService.updateOne(company);
        Info<Company> info = new Info<>();
        if(res==1){
            info.setCode(0);
        }
        else{
            info.setCode(1);
        }
        return info;
    }

    //后台删除
    @RequestMapping(value = "/delete/{cid}", method = RequestMethod.GET)
    @ResponseBody
    public Info<Company> deleteOne(ModelAndView mv, @PathVariable("cid") int cid){
        int res = companyService.deleteOne(cid);
        Info<Company> info = new Info<>();
        if(res==1){
            info.setCode(0);
        }
        else{
            info.setCode(1);
        }
        return info;
    }
}
